package com.study.rpc;

import com.study.rpc.service.StudentService;
import com.study.rpc.service.impl.StudentServiceImpl;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @version V1.0
 * @title: RpcServer
 * @description: ${DESCRIPTION}
 * @author: wanghuilong
 * @date: 2019-11-11 15:13
 * @copyright: http://fingo.com.my/
 */
public class RpcServer {

    private ServerSocket serverSocket;

    private int servPort;

    public RpcServer(int port) throws IOException {
        this.servPort = port;
        serverSocket = new ServerSocket(this.servPort);
    }

    public void start() {
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5, 10, 200
                , TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
        while (true) {
            try {
                Socket socket = serverSocket.accept();
                ServerService service = new ServerService(socket);
                service.registerService(StudentService.class, StudentServiceImpl.class);
                threadPool.execute(service);
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }

}
